var menuArr = "";
$(function() {
	var help = $("#container1");
	var InstallButton = $("#InstallButton");
	var ibOffset = InstallButton.offset();
	help.offset({
		left: (ibOffset.left),
		top: (ibOffset.top - help.height() - ($.browser.msie ? 10 : 25))
	});

	var menuStr = getModelXml();
	menuArr = menuStr.split(",");
	if (menuArr.length != 0) {
		var flag = 0;
		var menuTable = $("#menu");
		menuTable.empty();
		for (var i = 0; i < Math.ceil(menuArr.length / 2); i++) {
			var tr = $("<tr></tr>");
			for (var j = 0; j < 3; j++) {
				var td = $("<td></td>");
				if (j % 2 == 0 && flag < menuArr.length && menuArr[flag]) {
					td.addClass("buttonTD");
					td.attr({
						"align": "center",
						"valign": "center"
					});
					var thisArr = menuArr[flag].split("/");
					td.html("<a href='javascript:a();'>" + thisArr[1] + "</a>");
					td.attr("id", thisArr[0]);
					td.click(toPastoralUrlByTD);
					tr.append(td);
					flag++;
				} else {
					td.html("&nbsp;");
					tr.append(td);
				}
			}
			menuTable.append(tr);
		}
	}

	var td = $("#lastDIV").find("td");
	td.click(toPastoralUrlByTD);
	$("#appletini").show();
	var training = $("#training");
	training.html(getITraning());
});
function setMainModel(histValue) {
	//if(histValue){
	setHistButton(histValue);
	$("#lastDIV").addClass("lastClickTD");
	//$("#lastDIV").click(toPastoralUrlByLast);
	//}
}

function toPastoralUrlByTD() {
	var idValue = $(this).attr("id");
	if (idValue) {
		toPastoralUrl(idValue);
	}
}
function setHistButton(histValue) {
	var td = $("#lastDIV").find("td");
	var pmsModel = histValue;
	var s = $("#menu").find("#" + histValue);
	if (s.is("td")) {
		td.html("<a href='javascript:a();'>" + s.text() + "</a>");
		td.attr("id", histValue);
	} else {
		var defaultValue = menuArr[0].split("/");
		td.html("<a href='javascript:a();'>" + defaultValue[1] + "</a>");
		td.attr("id", defaultValue[0]);
	}
}
function a() {
	return;
}
function getIModelXml() {
	var serxml1 = getICookie("modelxml");
	if (serxml1 != null) {
		return getIReplaceStr(serxml1);
	} else return getIReplaceStr( < %="'" + serverxml[2] + "'" % >);
}
function getIReplaceStr(serxml) {
	var result = serxml;
	while (result.lastIndexOf(":") > -1 || result.lastIndexOf("+") > -1) {
		result = result.replace(':', '=');
		result = result.replace('+', ';');
	}
	result = result.replace('|', ':');
	return result;
}
function getICookie(name) {
	var cookieArr = document.cookie.split(";");
	for (var i = 0; i < cookieArr.length; i++) {
		var testArr = cookieArr[i].split("=");
		if ($.trim(testArr[0]) == name) {
			return testArr[1];
		}
	}
}
function getITraning() {
	var serverxml = getIReplaceStr(getICookie("serverxml1"));
	serverxml = serverxml.substring(serverxml.indexOf("training=") + 9);
	serverxml = serverxml.substring(0, serverxml.indexOf(";"));
	return serverxml;
}